﻿/*
///    This file is part of BannerLib4Net.
///
///    BannerLib4Net is free software: you can redistribute it and/or modify
///    it under the terms of the GNU General Public License as published by
///    the Free Software Foundation, either version 3 of the License, or
///    (at your option) any later version.

///    BannerLib4Net is distributed in the hope that it will be useful,
///    but WITHOUT ANY WARRANTY; without even the implied warranty of
///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///    GNU General Public License for more details.
///
///    You should have received a copy of the GNU General Public License
///    along with BannerLib4Net.  If not, see <http://www.gnu.org/licenses/>.
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using BannerLib4Net.Data.Validation;

namespace BannerLib4Net.SqlServer.Extensions
{
    public static class DataCommandExtensions
    {
        public static void AddSqlServerTableParameter(this SqlCommand cmd, DataTable tbl, String parameterName)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.Structured;
            prm.Value = tbl;
            cmd.Parameters.Add(prm);
        }

        public static void AddNVarcharParameter(this SqlCommand cmd, String parameterName, String parameterValue, int size = 255)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.NVarChar;
            prm.Value = DataInputValidator.CheckInputStringValue(parameterValue);
            prm.Size = size;


            cmd.Parameters.Add(prm);
        }

        public static void AddNVarcharMaxParameter(this SqlCommand cmd, String parameterName, String parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.NVarChar;
            prm.Value = DataInputValidator.CheckInputStringValue(parameterValue);
            prm.Size = -1;
            cmd.Parameters.Add(prm);
        }

        public static void AddVarcharParameter(this SqlCommand cmd, String parameterName, String parameterValue, int size = 255)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.VarChar;
            prm.Value = DataInputValidator.CheckInputStringValue(parameterValue);
            prm.Size = size;

            cmd.Parameters.Add(prm);
        }

        public static void AddVarcharMaxParameter(this SqlCommand cmd, String parameterName, String parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.VarChar;
            prm.Value = DataInputValidator.CheckInputStringValue(parameterValue);
            prm.Size = -1;
            cmd.Parameters.Add(prm);
        }

        public static void AddInt16Parameter(this SqlCommand cmd, String parameterName, Int16 parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.SmallInt;
            prm.Value = DataInputValidator.CheckInputInt16Value(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddInt32Parameter(this SqlCommand cmd, String parameterName, Int32 parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.Int;
            prm.Value = DataInputValidator.CheckInputInt32Value(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddInt64Parameter(this SqlCommand cmd, String parameterName, Int64 parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.BigInt;
            prm.Value = DataInputValidator.CheckInputInt64Value(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddBooleanParameter(this SqlCommand cmd, String parameterName, bool parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.Bit;
            prm.Value = parameterValue;
            cmd.Parameters.Add(prm);
        }

        public static void AddDecimalParameter(this SqlCommand cmd, String parameterName, decimal parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.Decimal;
            prm.Value = DataInputValidator.CheckInputDecimalValue(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddDoubleParameter(this SqlCommand cmd, String parameterName, double parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.Float;
            prm.Value = DataInputValidator.CheckInputDoubleValue(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddDateTimeParameter(this SqlCommand cmd, String parameterName, DateTime parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.DateTime;
            prm.Value = DataInputValidator.CheckInputDateTimeValue(parameterValue);
            cmd.Parameters.Add(prm);
        }

        public static void AddMoneyParameter(this SqlCommand cmd, String parameterName, decimal parameterValue)
        {
            SqlParameter prm = cmd.CreateParameter();
            prm.ParameterName = parameterName;
            prm.SqlDbType = SqlDbType.Money;
            prm.Value = DataInputValidator.CheckInputDecimalValue(parameterValue);
            cmd.Parameters.Add(prm);
        }





    }
}
